﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.AccessControl;
using Google.Documents;
using Google.Contacts;
using DataType;
using LocalView;
using System.IO;
using System.Diagnostics;
using System.Web.Mail;
using LicenseManager;
using Utilities;

namespace UILibrary
{
    public partial class MyGDocsForm : UserControl
    {
        //public ExcelLocalView m_LocalView;
        public ILocalView m_LocalView;
        public ThreadSafeUIOperations m_TS_UI;
        public TreeViewDocuments m_Tree;
        private Credentials m_Cred;
        private DbOperations m_dbOperations;
        TraceWrapper m_traceWrapper;
        System.Windows.Forms.Button m_copyBtn;

        public MyGDocsForm(ILocalView LocalView, bool disabled, DbOperations dbOperations, TraceWrapper traceWrapper)
        {
            InitializeComponent();

            m_LocalView = LocalView;
            m_dbOperations = dbOperations;
            m_traceWrapper = traceWrapper;

            m_Tree = new TreeViewDocuments(this.documentsView);
            m_TS_UI = new ThreadSafeUIOperations();
            readerRadioButton.Checked = true;
            //LoginProgress.Visible = true;
            UpdateBtn.Enabled = false;
            RefreshButton.Enabled = false;
            RegistrationLink.Visible = false;
            ShareButton.Enabled = false;
            FeedbackBtn.Enabled = false;
            SupportMsg.Text = Properties.Resources.SupportTemplate;

            Credentials cred = new Credentials(null, null);
            m_dbOperations.DoLoadCredentials(cred);

            if (cred.m_remember == true) {
                UserName.Text = cred.m_Cred.Username;
                Password.Text = cred.m_password;
                checkRemember.Checked = cred.m_remember; 
            }

            if (disabled)
            {
                UserName.Enabled = false;
                Password.Enabled = false;
                checkRemember.Enabled = false;
                LoginButton.Enabled = false;
                RegistrationLink.Visible = true;
            }
        }

        private void MyGDocsForm_Resize(object sender, EventArgs e)
        {
            this.MyGDocsTabControl.Size = this.Size;
        }

        private void AddListBoxItem(string Msg, bool CheckState)
        {
            this.ContactListBox.Items.Add(Msg, false);
        }

        //private void SwitchOnProgress(PictureBox pb) {
        //    pb.Visible = true; 
        //} 

        private void AddContact2UI(Contact contact)
        {
            if ((contact.ContactEntry.Emails.Count == 0) || (contact.ContactEntry.Emails[0].Address.Length == 0))
                return;
            else
                m_TS_UI.AddListBoxItem(this.ContactListBox, contact.ContactEntry.Emails[0].Address, false);
        }

        private void BuildContacts()
        {
            FeedWrapper<Contact> feed = new FeedWrapper<Contact>();
            m_LocalView.DoGetContacts(feed);
            foreach (Contact cnt in feed.m_Feed.Entries)
                AddContact2UI(cnt);
            //m_LocalView.DoGetContactsAsync(this.AddContact2UI);
        }

        private void BuildDocuments()
        {
            m_Tree.InitTree();
            FeedWrapper<Document> feed = new FeedWrapper<Document>();
            m_LocalView.DoGetDocuments(feed);

            foreach (Document entry in feed.m_Feed.Entries) m_Tree.AddDocumentToCollection(entry);

            m_Tree.ShowTree();
        }

        private void BuildDataStructure()
        {
            BuildContacts();
            BuildDocuments();
        }

        public void Login_Click(object sender, EventArgs e)
        {
            m_TS_UI.SetVisible(StatusMessage, false);
            m_TS_UI.SetEnabled(LoginButton, false);
            //LoginProgress.Visible = true;
            m_TS_UI.SetEnabled(UserName, false);
            m_TS_UI.SetEnabled(Password, false);
            m_TS_UI.SetEnabled(checkRemember, false);
            m_TS_UI.SetEnabled(RefreshButton, false);

            try
            {
                LicenseManager.ZohoLicenseManager lm = new ZohoLicenseManager("info@upriseapps.com", "mamamilaramu", m_dbOperations);
                lm.Login();
                if (lm.RegisteredContactBlocked())
                    throw new ZohoLicenseBlocked();

                m_Cred = new Credentials(UserName.Text, Password.Text);
                
                if (checkRemember.Checked) m_Cred.m_remember = true;
                else m_Cred.m_remember = false;

                ContactListBox.Items.Clear();
                m_Tree.CleanUp();

                m_TS_UI.SetEnabled(ShareButton, false);

                m_LocalView.DoLogin(m_Cred);

                // save credentials for future purposes.
                //if (m_Cred.m_remember)
                m_dbOperations.DoSaveCredentials(m_Cred);

                m_TS_UI.SetEnabled(ShareButton, true);
                m_TS_UI.SetForeColor(StatusMessage, Color.Black);
                m_TS_UI.SetText(StatusMessage, Properties.Resources.LoginSucceeded);

                m_TS_UI.SetEnabled(RefreshButton, true);
                m_TS_UI.SetEnabled(FeedbackBtn, true);

                BuildDataStructure();

                MyGDocsTabControl.SelectTab("Explorer"); 
            }
            catch (AuthenticationException ex)
            {
                Utilities.TraceWrapper.LogException("Login_Click error: " + ex.Message, ex);

                m_TS_UI.SetEnabled(FeedbackBtn, false);
                m_TS_UI.SetEnabled(ShareButton, false);
                m_TS_UI.SetForeColor(StatusMessage, Color.Red);
                m_TS_UI.SetText(StatusMessage, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetEnabled(RefreshButton, false);
            }
            catch (ZohoLicenseBlocked ex)
            {
                Utilities.TraceWrapper.LogException("Login_Click error: " + ex.Message, ex);

                MessageBox.Show("We are sorry but your Office In Cloud license is blocked. \n" +
                                 "Please contact Uprise Apps support: info@upriseapps.com for details."); 
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("Login_Click error: " + ex.Message, ex);

                m_TS_UI.SetEnabled(FeedbackBtn, false);
                m_TS_UI.SetEnabled(ShareButton, false);
                m_TS_UI.SetForeColor(StatusMessage, Color.Red);
                m_TS_UI.SetText(StatusMessage, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetEnabled(RefreshButton, false);
            }
            finally
            {
                m_TS_UI.SetVisible(StatusMessage, true);
                m_TS_UI.SetEnabled(LoginButton, true);
                m_TS_UI.SetEnabled(UserName, true);
                m_TS_UI.SetEnabled(Password, true);
                m_TS_UI.SetEnabled(checkRemember, true);
            }
        }

        private void ShareDocument()
        {
            ILocalView.Permission perm = ILocalView.Permission.writer;
            if (readerRadioButton.Checked)
                perm = ILocalView.Permission.reader;

            string[] users = new string[ContactListBox.CheckedItems.Count];
            for (int i = 0; i < ContactListBox.CheckedItems.Count; ++i)
            {
                users[i] = ContactListBox.CheckedItems[i].ToString();
            }

            m_LocalView.DoShareDocumentsAsync(ContactListBox.CheckedItems, perm);
        }

        private enum MailNotificationTypes { Share, Update }

        private void PrepareSendMessage(MailMessage mail)
        {
            //Ensure priority is set to "NORMAL
            mail.Priority = MailPriority.Normal;
            //  Use Authentication
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
            //Use this Username to Authenticate
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", this.m_Cred.m_Cred.Username);
            //Use this Password to Authenticate
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", this.m_Cred.m_password);
            //Use STARTTLS (some clients call this SSL) - Gmail help page indicated it used this
            mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            // assign outgoing gmail server
            SmtpMail.SmtpServer = "smtp.gmail.com";
            SmtpMail.Send(mail);  
        }

        private void SendNotification(AclFeed feed, 
                                      string CustomSubject, 
                                      string CustomBody, 
                                      string docURL, 
                                      string docName, 
                                      MailNotificationTypes mailType)
        {
            try
            {
                MailMessage mail = new MailMessage();

                foreach (AclEntry entry in feed.Entries)
                    mail.To += (entry.Scope.Value + ";");

                if (!this.m_Cred.m_Cred.Username.Contains("@"))
                    mail.From = this.m_Cred.m_Cred.Username + "@gmail.com";
                else
                    mail.From = this.m_Cred.m_Cred.Username;
                //System.Windows.Forms.MessageBox.Show("From = " + mail.From);


                if (CustomSubject.Trim().Length == 0)
                    mail.Subject = docName;
                else
                    mail.Subject = CustomSubject;

                switch (mailType)
                {
                    case MailNotificationTypes.Share:
                        mail.Body = Properties.Resources.MsgSharePrefix;
                        break;
                    case MailNotificationTypes.Update:
                        mail.Body = Properties.Resources.MsgUpdatePrefix;
                        break;
                }

                if (CustomBody.Trim().Length != 0)
                {
                    mail.Body += "\n";
                    mail.Body += CustomBody;
                }

                mail.Body += "\n";
                mail.Body += "\n";
                mail.Body += docName;
                mail.Body += "\n";
                mail.Body += docURL;
                mail.Body += "\n";
                mail.Body += "\n";
                mail.Body += "From Uprise Apps Office In Cloud";

                PrepareSendMessage(mail);
            }
            catch (System.Exception ex)
            {
                Utilities.TraceWrapper.LogException("SendNotification error: " + ex.Message, ex);
            }
        }

        public void ShareButton_Click(object sender, EventArgs e)
        {
            m_TS_UI.SetEnabled(ShareButton, false);
            m_TS_UI.SetVisible(StatusMsgShare, false);

            try
            {
                DocumentInfo info = new DocumentInfo(m_LocalView.GetDocumentPath(), m_LocalView.GetDocumentName());

                m_LocalView.DoLookupRemoteCopy(info);
                if (info.m_DocEntry == null)
                    m_LocalView.DoSaveDocumentInCloud();
                else
                    m_LocalView.m_DocInfo = info;

                this.ShareDocument();

                //in other case the getAclList returns wrong values instead of list shared users
                System.Threading.Thread.Sleep(2000);
                ShareInfoCollection infoShare = new ShareInfoCollection(m_LocalView.m_DocInfo.m_DocEntry);
                m_LocalView.DoGetDocumentAcl(infoShare);

                if (infoShare.m_Feed.Entries.Count() != 0 && checkBoxSharedNotification.Checked)
                    this.SendNotification(infoShare.m_Feed,
                                          this.ShareSubject.Text,
                                          this.ShareMsg.Text,
                                          this.m_LocalView.GetInviteUri(),
                                          m_LocalView.m_DocInfo.m_Name,
                                          MailNotificationTypes.Share);

                m_Tree.InitTree();
                FeedWrapper<Document> feed = new FeedWrapper<Document>();
                m_LocalView.DoGetDocuments(feed);

                foreach (Document entry in feed.m_Feed.Entries) m_Tree.AddDocumentToCollection(entry);

                m_Tree.ShowTree();
            }
            catch (GDataRequestException ex)
            {
                Utilities.TraceWrapper.LogException("ShareButton_Click error: " + ex.Message, ex);

                m_TS_UI.SetForeColor(StatusMsgShare, Color.Red);
                m_TS_UI.SetText(StatusMsgShare, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetVisible(StatusMsgShare, true);
                MessageBox.Show("Google Docs has encountered an API issue \n. In order to continue sharing operation \n please reopen the new uploaded document from Cloud.", "API Error");
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("ShareButton_Click error: " + ex.Message, ex);

                m_TS_UI.SetForeColor(StatusMsgShare, Color.Red);
                m_TS_UI.SetText(StatusMsgShare, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetVisible(StatusMsgShare, true);
                //Console.WriteLine(ex.Message);
            }
            finally
            {
                m_TS_UI.SetEnabled(ShareButton, true);
                m_TS_UI.SetEnabled(UpdateBtn, true);
            }
        }

        private void tabPage1_Enter(object sender, EventArgs e)
        {
            this.documentsView.Height = this.MyGDocsTabControl.Height - 60;
            this.documentsView.Width = this.MyGDocsTabControl.Width - 30;
        }

        private void processNewDocument(Document d, Document.DownloadType type, string fileName)
        {
            this.exportDialog.Filter = "Excel|*.xls|Excel 2007|*.xlsx";//|Comma seperated|*.csv";
            this.exportDialog.FileName = fileName;

            //CHECK THAT HAPPENS IF SAVE AS XLSX
            if (this.exportDialog.ShowDialog() == DialogResult.OK)
            {
                switch (d.Type)
                {
                    case Document.DocumentType.Spreadsheet:
                        type = Document.DownloadType.xls; break;
                }

                DownloadInfo info = new DownloadInfo(d, type);
                m_LocalView.DoDownloadDocument(info);

                if (info.m_Stream != null)
                {
                    try
                    {
                        CommonOperations.DoSaveDocumentContent(this.exportDialog.OpenFile(), info.m_Stream);
                       
                        DocumentInfo chk_info = new DocumentInfo(null, null);
                        chk_info.m_DocEntry = d.DocumentEntry;
                        chk_info.m_FullName = this.exportDialog.FileName;
                        m_dbOperations.DoRegisterLocalCopy(chk_info);

                        m_LocalView.DoOpenLocalCopyShared(this.exportDialog.FileName);
                    }
                    catch (IOException ex)
                    {
                        Utilities.TraceWrapper.LogException("processNewDocument error: " + ex.Message, ex);
                        MessageBox.Show(ex.Message);
                    }
                }
            }
        }

        private void processExistedDocument(Document d, Document.DownloadType type, string fileName)
        {
            this.exportDialog.FileName = fileName;

            Collision m_CollisionDialog = new Collision();
            DialogResult m_Result = m_CollisionDialog.ShowDialog();

            //if (m_Result == DialogResult.OK && m_CollisionDialog.m_ServerToLocal.Checked)
            if (m_Result == DialogResult.OK)
            {
                //Workbook wbCollision = null;
                bool is_collision_book_opened = m_LocalView.IsDocumentOpened(fileName);
                // is_document_opened() should be
                //foreach (Workbook wb in m_LocalView.m_App.Workbooks)
                //{
                //    string wName = wb.Name;
                //    string wFName = wb.FullName;
                //    if  (wb.FullName == fileName)
                //    {
                //        is_collision_book_opened = true;
                //        wbCollision = wb;
                //        break;
                //    }
                //}

                if (!m_CollisionDialog.m_LocalToServer.Checked)
                {
                    DownloadInfo info = new DownloadInfo(d, type);
                    m_LocalView.DoDownloadDocument(info);

                    if (info.m_Stream != null)
                    {
                        try
                        {
                            if (m_CollisionDialog.m_ServerToLocal.Checked)
                            {
                                // if collision's book is opened then close it
                                if (is_collision_book_opened)
                                    m_LocalView.CloseDocument(fileName);
                                    //wbCollision.Close(false, null, false);

                                CommonOperations.DoSaveDocumentContent(this.exportDialog.OpenFile(), info.m_Stream);
                                m_LocalView.DoOpenLocalCopyShared(this.exportDialog.FileName);
                            }
                            else if (m_CollisionDialog.m_Merge.Checked)
                            {
                                if (is_collision_book_opened)
                                    m_LocalView.ActivateDocument(fileName);
                                    //wbCollision.Activate();
                                else
                                    m_LocalView.DoOpenLocalCopyShared(this.exportDialog.FileName);

                                //merge works only with 
                                m_LocalView.MergeContent(this.exportDialog.FileName, info.m_Stream);
                            }

                            m_LocalView.UpdateDocumentInfo();
                        }
                        catch (IOException ex)
                        {
                            Utilities.TraceWrapper.LogException("processExistedDocument error: " + ex.Message, ex);
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
                //user prefers leave with local copy, if it's already opened then we do nothing
                else if (m_CollisionDialog.m_LocalToServer.Checked && !is_collision_book_opened)
                    m_LocalView.DoOpenLocalCopyNotShared(this.exportDialog.FileName);
            }
        }

        private void documentsView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            Document d = e.Node.Tag as Document;
            // fill the filter based on the document type
            if ((d == null) || (d.Type != Document.DocumentType.Spreadsheet)) 
                return;

            DocumentInfo chk_info = new DocumentInfo(null, null);
            chk_info.m_DocEntry = d.DocumentEntry;
            m_dbOperations.DoLookupLocalCopy(chk_info);

            Document.DownloadType type = Document.DownloadType.xls;
            //bool processResult = false;

            if (chk_info.m_FullName == null)
                processNewDocument(d, type, d.Title.Replace(".xlsx", ".xls"));
            else
                processExistedDocument(d, type, chk_info.m_FullName.Replace(".xlsx", ".xls"));

            UpdateBtn.Enabled = true;
        }

        private void Explorer_Resize(object sender, EventArgs e)
        {
            documentsView.Width = Explorer.Width - 30;
            documentsView.Height = Explorer.Height - 30;
        }

        private void documentsView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (documentsView.SelectedNode == null) return;
                Document d = e.Node.Tag as Document;
                if (d == null) return;
                if (d.Type == Document.DocumentType.Folder) return;

                ContextMenu treeContextMenu = new ContextMenu();
                treeContextMenu.MenuItems.Add("Open in Cloud", OnOpenInCloud);
                System.Drawing.Point pt = new System.Drawing.Point(documentsView.SelectedNode.Bounds.Left,
                                     documentsView.SelectedNode.Bounds.Bottom);
                try
                {
                    treeContextMenu.Show(documentsView, pt);
                }
                catch (Exception ex)
                {
                    Utilities.TraceWrapper.LogException("documentsView_NodeMouseClick error: " + ex.Message, ex);
                }
            }
        }

        private void OnOpenInCloud(Object sender, EventArgs e)
        {
            if (documentsView.SelectedNode == null) return;
            Document d = documentsView.SelectedNode.Tag as Document;
            if (d == null) return;
            DocumentInfo info = new DocumentInfo(null, null);
            info.m_DocEntry = d.DocumentEntry;

            m_LocalView.DoOpenDocumentInCloud(info);
        }

        private void documentsView_MouseDown(object sender, MouseEventArgs e)
        {
            SetSelectedNodeByPosition(documentsView, e.X, e.Y);

            if (documentsView.SelectedNode == null) { return; }

            if (e.Button == MouseButtons.Right) { return; }
        }

        private static void SetSelectedNodeByPosition(TreeView tv, int mouseX, int mouseY)
        {
            TreeNode node = null;

            try
            {
                System.Drawing.Point pt = new System.Drawing.Point(mouseX, mouseY);

                tv.PointToClient(pt);

                node = tv.GetNodeAt(pt);

                tv.SelectedNode = node;

                if (node == null) return;

                if (!node.Bounds.Contains(pt)) { return; }

            }
            catch(Exception ex) 
            {
                Utilities.TraceWrapper.LogException("SetSelectedNodeByPosition error: " + ex.Message, ex);
            }
        }

        public long LookAtContactsCount()
        {
            long l = ContactListBox.Items.Count;
            return l;
        }

        private void RefreshButton_Click(object sender, EventArgs e)
        {
            m_TS_UI.SetVisible(StatusMsgExplorer, false);
            
            try
            {
                BuildDocuments(); 
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("RefreshButton_Click error: " + ex.Message, ex);

                m_TS_UI.SetForeColor(StatusMsgExplorer, Color.Red);
                m_TS_UI.SetText(StatusMsgExplorer, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetVisible(StatusMsgExplorer, true);
            }
        }

        private void RegistrationLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Registration regForm = new Registration(m_dbOperations);
            if (regForm.ShowDialog() == DialogResult.OK)
            {
                UserName.Enabled = true;
                Password.Enabled = true;
                checkRemember.Enabled = true;
                LoginButton.Enabled = true;
                RegistrationLink.Visible = false;
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                Process.Start(Properties.Resources.AppUrl);
            }
            catch (Win32Exception ex)
            {
                Utilities.TraceWrapper.LogException("linkLabel1_LinkClicked error: " + ex.Message, ex);

                //nothing is registered to handle URLs, so let's use IE!
                Process.Start("IExplore.exe", Properties.Resources.AppUrl);
            } 
 
        }

        private void UpdateBtn_Click(object sender, EventArgs e)
        {
            m_TS_UI.SetEnabled(UpdateBtn, false);
            m_TS_UI.SetVisible(StatusMsgUpdate, false);

            try{
                //get_current_document_path
                //get_current_document_name
                DocumentInfo info = new DocumentInfo(m_LocalView.GetDocumentPath(),
                                                     m_LocalView.GetDocumentName());
                m_LocalView.DoLookupRemoteCopy(info); 
                if (info.m_DocEntry != null)
                {
                    m_LocalView.m_DocInfo = info; 
                    m_LocalView.DoUpdateDocument();

                    ShareInfoCollection infoShare = new ShareInfoCollection(m_LocalView.m_DocInfo.m_DocEntry);
                    m_LocalView.DoGetDocumentAcl(infoShare);

                    if (infoShare.m_Feed.Entries.Count() != 0 && checkBoxUpdateNotification.Checked)
                        this.SendNotification(infoShare.m_Feed,
                                              this.UpdSubject.Text,
                                              this.UpdMsg.Text,
                                              this.m_LocalView.GetInviteUri(),
                                              m_LocalView.m_DocInfo.m_Name,
                                              MailNotificationTypes.Update);
                }
            }
            catch (GDataRequestException ex)
            {
                Utilities.TraceWrapper.LogException("UpdateBtn_Click error: " + ex.Message, ex);

                m_TS_UI.SetForeColor(StatusMsgUpdate, Color.Red);
                m_TS_UI.SetText(StatusMsgUpdate, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetVisible(StatusMsgUpdate, true);
            }
            catch (Exception ex)
            {
                Utilities.TraceWrapper.LogException("UpdateBtn_Click error: " + ex.Message, ex);

                m_TS_UI.SetForeColor(StatusMsgUpdate, Color.Red);
                m_TS_UI.SetText(StatusMsgUpdate, Properties.Resources.ErrorPrefix + ex.Message);
                m_TS_UI.SetVisible(StatusMsgUpdate, true);
            }
            finally
            {
                m_TS_UI.SetEnabled(UpdateBtn, true);
            }
        }

        private void MyGDocsTabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            //reset all errors messages
            m_TS_UI.SetVisible(StatusMessage, false);
            m_TS_UI.SetVisible(StatusMsgShare, false);
            m_TS_UI.SetVisible(StatusMsgUpdate, false);
            m_TS_UI.SetVisible(StatusMsgExplorer, false);
        }

        private void LoginButton_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
                this.Login_Click(sender, null); 
            }

        }

        private void SendBtn_Click(object sender, EventArgs e)
        {
          try
            {
                MailMessage mail = new MailMessage();

                mail.To = "testoffice10@gmail.com";

                if (!this.m_Cred.m_Cred.Username.Contains("@"))
                    mail.From = this.m_Cred.m_Cred.Username + "@gmail.com";
                else
                    mail.From = this.m_Cred.m_Cred.Username;

                if (SupportSubj.Text.Trim().Length == 0)
                    mail.Subject = Properties.Resources.FeedbackSubject;
                else
                    mail.Subject = SupportSubj.Text;

                mail.Body = Properties.Resources.MsgFeedbackPrefix;

                if (SupportMsg.Text.Trim().Length != 0)
                {
                    mail.Body += "\n";
                    mail.Body += SupportMsg.Text;
                }

                mail.Body += "\n";
                mail.Body += "With best wishes From Uprise Apps Office In Cloud.";

                m_traceWrapper.shutdownTraceListener();
                mail.Attachments.Add(new MailAttachment(Utilities.Path.logPath));

                PrepareSendMessage(mail);
            }
            catch (System.Exception ex)
            {
                m_traceWrapper.initTraceListener();
                Utilities.TraceWrapper.LogException("SendBtn_Click error: " + ex.Message, ex);
            }
        }

    }
}
